package com.atguigu.sms.strategy;

import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.sms.model.param.SmsPlaceHolderParameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Set;

/**
 * 只要占位符参数匹配了短信模板中的任意一个占位符key,就允许发送
 *
 */
@Slf4j
public class SmsSendAnyMatchStrategy implements SmsSendRejectStrategy {
    @Override
    public void reject(SmsTemplateContext context, SmsPlaceHolderParameter parameter) {
        Set<String> parameterSet = getParameterSet(parameter);
        if (CollectionUtils.intersection(context.getPlaceHolderKeySet(), parameterSet).size() <= 0) {
            log.error("短信占位符替换参数与短信模板完全不匹配,templateContent = {},parameter = {}", context.getTemplateContent(), parameter);
            throw new GuliException(20001, "短信占位符替换参数与短信模板完全不匹配");
        }
    }
}
